﻿using MangoMes.Commont.DapperHelps;
using MangoMes.Commont.DTOs;
using MangoMes.Commont.Infrastructures.Interfaces;
using MediatR;

namespace MangoMes.Write.API.Applications.PurchaseStorags
{
    public class PurchaseStorageListHandler : IRequestHandler<PurchaseStorageListCommand, PageDTO<PurchaseStorageDTO>>
    {
        private readonly IBateRepository<PurchaseStorageDTO> purchdto;
        private readonly ILogger<PurchaseStorageListHandler> logger;
        public PurchaseStorageListHandler(IBateRepository<PurchaseStorageDTO> purchdto, ILogger<PurchaseStorageListHandler> logger)
        {
            this.purchdto = purchdto;
            this.logger = logger;
        }
        /// <summary>
        /// 采购入库列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<PageDTO<PurchaseStorageDTO>> Handle(PurchaseStorageListCommand request, CancellationToken cancellationToken)
        {
            try
            {
                DapperHelper helper = new DapperHelper();
                PageDTO<PurchaseStorageDTO> dto = new PageDTO<PurchaseStorageDTO>();
                PurchaseStorageDTO purch = new PurchaseStorageDTO();
                string conn = helper.MangoMesWrite;
                string sql = "SELECT a.PurchaseId,a.PurchaseNo,a.PurchaseName,Supplier.SupplierName,a.PurchaseDate,a.PurchaseIndentId,a.ReceiptState,a.Remark,arrivalnotice.ArrivalNoticeId,Supplier.SupplierId FROM purchasestorage a inner join Supplier on a.supplierid=Supplier.supplierid join arrivalnotice on a.ArrivalNoticeId=arrivalnotice.ArrivalNoticeId  where a.isNo=0 ";
                var result = purchdto.GetAll(sql, purch, conn).ToList();
                if (!string.IsNullOrEmpty(request.PurchaseNo))
                {
                    result = result.Where(x => x.PurchaseNo == request.PurchaseNo).ToList();
                }
                if (!string.IsNullOrEmpty(request.PurchaseName))
                {
                    result = result.Where(x => x.PurchaseName.Contains(request.PurchaseName)).ToList();
                }
                if (!string.IsNullOrEmpty(request.SupplierName))
                {
                    result = result.Where(x => x.SupplierName.Contains(request.SupplierName)).ToList();
                }
                if (request.PurchaseIndentId > 0)
                {
                    result = result.Where(x => x.PurchaseIndentId == request.PurchaseIndentId).ToList();
                }

                if (request.PurchaseDate != null)
                {
                    result = result.Where(x => x.PurchaseDate == request.PurchaseDate).ToList();
                }
                dto.TotalCount = result.Count();
                dto.PageCount = (int)Math.Ceiling((result.Count() * 1.0) / request.pageSize);
                dto.Data = result.OrderBy(x => x.PurchaseId).Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
                logger.LogInformation("成功");
                return dto;
            }
            catch (Exception ex)
            {
                logger.LogError("失败" + ex);
                throw;
            }
          
        }
    }
}
